<html>
    <head>
        <title>Turbulenz - Getting Started Guide - API Example</title>
        <script src="jslib/debug.js"></script>  
        <script src="jslib/webgl/turbulenzengine.js"></script>
        <script src="jslib/webgl/graphicsdevice.js"></script>
        <script src="jslib/draw2d.js"></script>
        <script src="jslib/canvas.js"></script>
        <script src="jslib/utilities.js"></script>
        <script src="jslib/textureeffects.js"></script>
        <script type="text/javascript">
        TurbulenzEngine = WebGLTurbulenzEngine.create({
            canvas: document.getElementById("canvas")
            });
        </script>
        <script src="jslib/vmath.js"></script>
        

    </head>
    <body>
        <canvas id="canvas" width="640px" height="480px"/>
        <script>
            TurbulenzEngine = WebGLTurbulenzEngine.create({
            canvas: document.getElementById("canvas")
            });
            
            var graphicsDevice = TurbulenzEngine.createGraphicsDevice({});
            
            var mathDevice = TurbulenzEngine.createMathDevice({});
            var canvas = Canvas.create(graphicsDevice, mathDevice);       
            var draw2D = Draw2D.create({§
            graphicsDevice: graphicsDevice
            });     
            
            var viewportRect = [0, 0, 640, 480];
            /////// Canvas2D ////////           
            //OK: ctx === Turbulenz Canvas Context
            var ctx = canvas.getContext('2d');

            ////// TileMap Variabels
            var tileHeight = 32;
            var tileWidth = 32;
            var mapX = 256;
            var mapY = 128; 
            
            var r = 1.0, g = 0.0, b = 0.0, a = 1.0;
            var bgColor = [r, g, b, a];
            
            var x1 = 50;
            var y1 = 50;
            var x2 = graphicsDevice.width - 50;
            var y2 = graphicsDevice.height - 50;
            
            var rectangle = [x1, y1, x2, y2];
            var drawObject = {
                color: [0.0, 0.0, 0.0, 1.0],
                destinationRectangle: rectangle
            };
            
            var map = Array([1,0,0,1,1,2],[0,0,0,1,2,2],[2,2,0,0,1,1],[1,1,1,2,1,0]);
            var map2 = Array([2, 2, 1, 1, 1],[ 2, 2, 2, 2, 1],[ 1, 2, 2, 2, 1],[ 1, 2, 2, 2, 2],[ 1, 2, 2, 2, 2]);
            
            var sprite = Draw2DSprite.create({
                width: 128,
                height: 128,
                x: graphicsDevice.width / 2,
                y: graphicsDevice.height / 1.5,
                color: [1.0, 1.0, 1.0, 1.0],
                textureRectangle: [128, 128, 256, 0]
            });
            
            var backgroundSprite = Draw2DSprite.create({
            width: 640,
            height: 640,
            x: graphicsDevice.width / 2,
            y: graphicsDevice.height / 2,
            color: [1.0, 1.0, 1.0, 1.0],
            });                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            
            var tileArray = new Array();
            
            var crateImageParameters = {
                src: "img/tile4.png",
                mipmaps: true,
            };
            
            var crateImage2Parameters = {
                src: "img/tile1.png",
                mipmaps: true,
            };
            
            var crateImage3Parameters = {
                src: "img/tile3.png",
                mipmaps: true,
            };
            
            tileArray[1] = graphicsDevice.createTexture(crateImageParameters);
            tileArray[2] = graphicsDevice.createTexture(crateImage2Parameters);
            tileArray[3] = graphicsDevice.createTexture(crateImage3Parameters);
            
            function update() {                
                sprite.setOrigin([128,0]);
                sprite.setOrigin([128,0]);
                /* Begin render */                                                
                if (graphicsDevice.beginFrame())
                {
//                    graphicsDevice.clear(bgColor, 1.0);
//                    
//                    draw2D.begin('alpha');
//                    draw2D.drawSprite(sprite);                    
//                    draw2D.end();                         
                    
                        ctx.beginFrame(graphicsDevice, viewportRect);                   
                    
                        for(i = 0; i < map2.length; i++){
                            for(j = 0; j < map2[i].length; j++){
                                var drawTile = map2[i][j];
                                var xpos = (i - j) * tileHeight + mapX;
                                var ypos = (i + j) * tileWidth / 2 + mapY;                            
                                ctx.drawImage(tileArray[drawTile],xpos,ypos,64,64);  
                            }
                        } 
                    
                    ctx.endFrame();
                    
                    graphicsDevice.endFrame();                   
                }
            }
            
            TurbulenzEngine.setInterval(update, 1000/60);     
            
            
        </script>
        
    </body>
</html>